<extend name="Layout:layout"/>

<block name="css">
    <link rel="stylesheet" href="__PUBLIC__/metis/assets/lib/plupload/jquery.plupload.queue/css/jquery.plupload.queue.css">

    <style type="text/css"></style>
</block>

<block name="srcjs">
    <script type="text/javascript" src=""></script>
</block>

<block name="content">
    <div class="box">

        <header class="dark">
            <div class="icons"> <i class="fa fa-check"></i>
            </div>
            <h5>新增图片文档</h5>
        </header>
        <div class="row">
            <div class="col-lg-12">
                <div class="box">
                    <div class="block">
                        <form class="form-horizontal" id="pdocForm">
                            <div class="form-group">
                                <label for="pdoc_sn" class="col-lg-4 control-label">文档编号</label>
                                <div class="col-lg-4">
                                    <input type="text" class="input-read" name="pdoc_sn" id="pdoc_sn" readonly="true" value="{$pdoc_sn}"></div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4">文档名称</label>
                                <div class="col-lg-4">
                                    <input type="text" class="form-control" name="pdoc_name" id="pdoc_name" required="required"></div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4">文档描述</label>
                                <div class="col-lg-4">
                                    <input type="text" class="form-control" name="pdoc_describe" id="pdoc_describe" required="required"></div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4">文档类型</label>
                                <div class="col-lg-4">
                                    <select id="pdoc_catagory" name="pdoc_catagory" class="form-control">
                                        <volist name="catagory" id="item">
                                            <option value="{$item.name}">{$item.name}</option>
                                        </volist>
                                      </select>
                                </div>
                                <div class="col-lg-4">
                                    <a href="{:U('addCatagoryPage')}" class="btn btn-default btn-sm">编辑</a>
                                </div>
                            </div>
                            
                            
                            <div id="uploader"></div>
                            <div class="form-group text-center">
                                <button type="button" id="btnUploadPic" class="btn btn-info">上传提交</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

    </div>

</block>

<block name="customjs">

    <script src="__PUBLIC__/metis/assets/lib/plupload/plupload.full.min.js"></script>
    <script src="__PUBLIC__/metis/assets/lib/plupload/jquery.plupload.queue/jquery.plupload.queue.min.js"></script>
    <script src="__PUBLIC__/metis/assets/lib/plupload/langs/zh_CN.js"></script>

    <script type="text/javascript">

        function checkUploadFiles(status_array) {
            if (status_array.length !== 0) {
                for (var i = 0; i < status_array.length; i++) {
                    if (status_array[i] === 0) {
                        return false;
                    }
                }
            } else {
                return false;
            }
            return true;
        }

    </script>

    <script type="text/javascript">
        $(function () {
            //var flag = 0;

            var status_array = new Array();

            $("#uploader").pluploadQueue({
                runtimes: 'html5,html4',
                url: "{:U('upload')}" + "?pdoc_sn=" + $("#pdoc_sn").val(),
                unique_names: true,
                filters: {
                    // Maximum file size
                    max_file_size: '2mb',
                    // Specify what files to browse for
                    mime_types: [
                        {title: "Image files", extensions: "jpg,gif,png"}
                    ]
                },
                init: {
                    FileUploaded: function (up, file, result) {
                        var obj = $.parseJSON(result.response);
                        status_array.push(obj.status);
                    }
                }
            });

            var uploader = $('#uploader').pluploadQueue();

            $("#uploader_browse").next().hide();

            $("#btnUploadPic").click(function (event) {
                /* Act on the event */

                var pdoc_name = $.trim($("#pdoc_name").val());
                if (pdoc_name === "") {
                    alert("请先输入文档名称");
                    $("#pdoc_name").focus();
                    return;
                }

                if (uploader.files.length > 0) {
                    uploader.start();

                    uploader.bind('UploadComplete', function (up, files) {

                        if (uploader.total.uploaded === uploader.files.length) {

                            if (checkUploadFiles(status_array)) {
                                var sendData = {
                                    'pdoc_sn': $("#pdoc_sn").val(),
                                    'pdoc_name': pdoc_name,
                                    'pdoc_create_person': "{$_SESSION['current_user']['username']}",
                                    'pdoc_describe': $.trim($("#pdoc_describe").val()),
                                    'pdoc_catagory': $("#pdoc_catagory").val(),
                                    'pdoc_pic_number': uploader.files.length
                                };

                                //console.log(sendData);
                                $.post("{:U('savePicDoc')}", sendData).done(function (responseData) {
                                    if (responseData.status === 1) {
                                        alert(responseData.info);
                                        location.href = responseData.jumpUrl;
                                    } else {
                                        alert(responseData.info);
                                        location.href = responseData.jumpUrl;
                                    }

                                });
                            }

                            else {

                                alert("上传图片命名有误！");
                                $.post("{:U('delPicFiles')}", {'pdoc_sn': $("#pdoc_sn").val()}).done(function (responseData) {
                                    if (responseData.status === 1) {
                                        //alert(responseData.info);
                                        location.href = responseData.jumpUrl;
                                    } else {
                                        //alert(responseData.info);
                                        location.href = responseData.jumpUrl;
                                    }
                                });
                            }

                        } else {
                            alert("上传失败！");
                        }

                    });

                } else {
                    alert('请先添加图片....');
                }
            });

        });

    </script>
    

</block>